import requests
import json
from jsonpath import jsonpath
import time
from openpyxl import workbook

wb = workbook.Workbook()  # 创建excel对象
ws = wb.active  # 激活表对象
# 创建表头
ws.append(['名称', '价格', '涨跌额', '市值', '交易额', '流动性', '交易次数', '持币地址数', '持币地址'])
url1 = "https://api.msgsender.org/api/getPairs"
payload = '{\"chainType\":3,\"checkTime\":0,\"dexType\":1,\"pageId\":1,\"pageSize\":200,\"sort\":0,\"type\":2}'
headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh,zh-CN;q=0.9,en;q=0.8',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json;charset=UTF-8;',
    'Origin': 'https://tools.msgsender.io',
    'Referer': 'https://tools.msgsender.io/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"'
}
response = requests.request("POST", url1, headers=headers, data=payload)
json_data = json.loads(response.text)
pairAddress = jsonpath(json_data, "$..pairAddress")
name1 = jsonpath(json_data, "$..token0Symbol")
name2 = jsonpath(json_data, "$..token1Symbol")
chibidizhi = jsonpath(json_data, "$..token0")
x = 1
error = []
for pairAddress, name1, name2, chibidizhi in zip(pairAddress, name1, name2, chibidizhi):
    url = "https://api.msgsender.org/api/get24DayLine"
    payload = '{"pairAddress":"%s","startTime":0,"chainType":3,"type":4}' % pairAddress
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh,zh-CN;q=0.9,en;q=0.8',
        'Connection': 'keep-alive',
        'Content-Type': 'application/json;charset=UTF-8;',
        'Origin': 'https://tools.msgsender.io',
        'Referer': 'https://tools.msgsender.io/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    html_text = response.text
    html_p = json.loads(html_text)
    price = jsonpath(html_p, "$..close")
    increase = jsonpath(html_p, "$..increase")
    shizhi = jsonpath(html_p, "$..price")
    jiaoyie = jsonpath(html_p, "$..goldPrice")
    liudongxing = jsonpath(html_p, "$..totalLiquidity")
    jiaoyicishu = jsonpath(html_p, "$..volume")
    chibidizhishu = jsonpath(html_p, "$..holders")
    name = name1 + '\\' + name2
    # print('==================================')
    print(x)
    x += 1
    # print(name)
    # print(price)
    # print(increase)
    # print(shizhi)
    # print(jiaoyie)
    # print(liudongxing)
    # print(jiaoyicishu)
    # print(chibidizhishu)
    # print(chibidizhi)
    # print(response.text)
    error_message = jsonpath(html_p, "$..error")
    if error_message != False:
        print(error_message[0])
        error.append(error_message)
        continue
    for price, increase, shizhi, jiaoyie, liudongxing, jiaoyicishu, chibidizhishu in zip(price, increase, shizhi, jiaoyie, liudongxing, jiaoyicishu, chibidizhishu):
        my_list = [name, price, increase, shizhi, jiaoyie, liudongxing, jiaoyicishu, chibidizhishu, chibidizhi]
        print(my_list)
        ws.append(my_list)
        wb.save("股票详情.xlsx")
        wb.close()
print('全部完成')
